<?php
namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
use App\Models\Users;
use App\Models\Scanscore;
use App\Models\Memberorder;
use App\Models\Corplist;
use App\Models\Shortgroup;
use App\Services\OutputService;
use App\Services\MiniappService;
use Exception,Log,Config;

class LoginController extends Controller
{
    public function getOpenid(Request $request)
    {
        if (empty($request->code)) return OutputService::error("非法请求,请稍后重试！");
        $miniapp = Config::get("miniapps.".$request->appid);
        if (empty($miniapp)) return OutputService::error("非法请求,请稍后重试！");
        $codeinfo = MiniappService::code2Session($request->code,$miniapp["app_id"],$miniapp["secret"]);
        if (!isset($codeinfo["openid"])) return response()->json(["status"=>"error","code"=>"2001","msg"=>"登录异常,请联系客服！"]);
        $openid = $codeinfo["openid"];
        $unionid = isset($codeinfo["unionid"]) ? $codeinfo["unionid"] : "";
        return OutputService::success("SUCCESS",["openid"=>$openid,"unionid"=>$unionid]);
    }

    public function getPhone(Request $request)
    {
        if (empty($request->openid)) return OutputService::error("参数错误!");
        if (empty($request->code)) return OutputService::error("参数错误!");
        if (empty($request->appid)) return OutputService::error("参数错误!");
        $accessToken = MiniappService::getAccessToken($request->appid);
        if ($accessToken["status"] == "error") return OutputService::error("接口失败！");
        $phoneResult = MiniappService::getUserPhoneNumber($accessToken["access_token"],$request->code,$request->openid);
        if ($phoneResult["errcode"] == 0) {
            $phone = $phoneResult["phone_info"]["purePhoneNumber"];
            return OutputService::success("SUCCESS",["phone"=>$phone]);
        } else {
            return OutputService::error("获取手机号失败!");
        }
    }

    public function corpQrcode(Request $request)
    {
        if (empty($request->openid)) return OutputService::error("参数错误!");
        if (empty($request->corp_id)) return OutputService::error("参数错误!");
        if (empty($request->appid)) return OutputService::error("参数错误!");
        $accessToken = MiniappService::getAccessToken($request->appid);
        if ($accessToken["status"] == "error") return OutputService::error("接口失败！");
        $imageData = MiniappService::getWxaCode($accessToken["access_token"],"pages/corp/corp_detial?corp_id=".$request->corp_id);
        $filename = $request->corp_id.'.png';
        file_put_contents(public_path("qrcodes/{$filename}"),$imageData);
        $qrcode = url("qrcodes/{$filename}");
        return OutputService::success("SUCCESS",["qrcode"=>$qrcode]);
    }

    public function userLogin(Request $request)
    {
        if (empty($request->code)) return OutputService::error("非法请求,请稍后重试！");
        $miniapp = Config::get("miniapps.".$request->appid);
        if (empty($miniapp)) return OutputService::error("非法请求,请稍后重试！");
        $codeinfo = MiniappService::code2Session($request->code,$miniapp["app_id"],$miniapp["secret"]);
        if (!isset($codeinfo["openid"])) return response()->json(["status"=>"error","code"=>"2001","msg"=>"登录异常,请联系客服！"]);
        $openid = $codeinfo["openid"];
        $unionid = isset($codeinfo["unionid"]) ? $codeinfo["unionid"] : "";
        $users = Users::getUserByOpenid($openid);
        if (empty($users)) {
            Users::usersAdd($openid,$request->appid,"","","");
            $group_id = md5($openid.time().rand(1000,9999));
            Shortgroup::shortGroupAdd($openid,$group_id,"默认分组");
            $users = Users::getUserByOpenid($openid);
        }
        $corpCount = Corplist::userCorpCount($openid);
        $users->corp_number = $corpCount;
        $memberOrder = Memberorder::userLastMember($openid);
        $users->end_time = empty($memberOrder) ? "" : $memberOrder->end_time;
        $users->pay_status = 2;
        return OutputService::success("SUCCESS",["users"=>$users]);
    }

    public function userRefresh(Request $request)
    {
        if (empty($request->openid)) return OutputService::error("非法请求,请稍后重试！");
        $openid = $request->openid;
        $users = Users::getUserByOpenid($openid);
        $corpCount = Corplist::userCorpCount($openid);
        $users->corp_number = $corpCount;
        $memberOrder = Memberorder::userLastMember($openid);
        $users->end_time = empty($memberOrder) ? "" : $memberOrder->end_time;
        $users->pay_status = 2;
        return OutputService::success("SUCCESS",["users"=>$users]);
    }

    public function corpType(Request $request)
    {
        if (empty($request->openid)) return OutputService::error("非法请求,请稍后重试！");
        $openid = $request->openid;
        $corp_type = $request->corp_type;
        Users::userCorpType($openid,$corp_type);
        return OutputService::success("SUCCESS");
    }
}